<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/GPL-3.0
 * @link https://www.gougucms.com
 */

declare (strict_types = 1);

namespace app\home\controller;

use app\base\BaseController;
use app\home\model\Filiales;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Filiale extends BaseController
{
    public function index()
    { if (request()->isAjax()) {
        $param = get_params();
        $where = array();
        $whereOr = array();
        if (!empty($param['keywords'])) {
            $where[] = ['a.name', 'like', '%' . $param['keywords'] . '%'];
        }
        $where[] = ['a.delete_time', '=', 0];

        $model = new Filiales();
        $list = $model->get_list($param, $where, $whereOr);

        return table_assign(0, '', $list);
    } else {
        $param = get_params();

        return view();
    }
    }

    //编辑配置信息
    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            if (!empty($param['id']) && $param['id'] > 0) {
                $param['update_time'] = time();
                $res = Db::name('filiales')->strict(false)->field(true)->update($param);
                if ($res) {
                    add_log('edit', $param['id'], $param);
                }
            }else{
                $param['create_time'] = time();
                $insertId = Db::name('filiales')->strict(false)->field(true)->insertGetId($param);
                if ($insertId) {
                    add_log('add', $insertId, $param);
                }
                return to_assign();
            }
            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            $detail = Db::name('filiales')->where('id',$id)->find();
            View::assign('detail', $detail);
            View::assign('id', $id);
            return view();

        }
    }

    //删除
    public function report_delete()
    {
        if (request()->isDelete()) {
            $id = get_params("id");
            $data['id'] = $id;
            $data['delete_time'] = time();
            if (Db::name('filiales')->update($data) !== false) {
                add_log('delete', $id);

                return to_assign(0, "删除成功");
            } else {
                return to_assign(1, "删除失败");
            }
        } else {
            return to_assign(1, "错误的请求");
        }
    }
	

}
